package com.wpw.dream.threadpool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class SynchronousQueueThreadTest implements Runnable {
	
	private Long i;
	
	
	@Override
	public void run() {
		System.out.println("任务" + i);
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public SynchronousQueueThreadTest(Long i) {
		this.i = i;
	}
	
	public static void main(String[] args) {
	
		ScheduledExecutorService newScheduledThreadPool1 = Executors.newScheduledThreadPool(2);
		
//		BlockingQueue<Runnable> workQueue = new SynchronousQueue<>();
		BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue(2);
		ThreadPoolExecutor newScheduledThreadPool = new ThreadPoolExecutor(1, 5, 0, TimeUnit.SECONDS, workQueue);
		
		for (long i = 1; i <= 20; i++) {
			System.out.println(i);
			newScheduledThreadPool.execute(new SynchronousQueueThreadTest(i));
		}
		
		System.out.println("jies");
		newScheduledThreadPool.shutdown();
	}


}
